const SpiderBaseClass = require('./SpiderBaseClass');

const listSize = 35;//列表页面有多少个主播信息
class SpiderLongZhu extends SpiderBaseClass {

  /**
   * 获取第一页列表
   */
  static firstListPage() {
    return undefined;
  }

  /**
   * 获取其它页码的列表页面
   * @param page
   */
  static async getListPage(page) {
    const res = await SpiderLongZhu.get(SpiderLongZhu.listPageUrl(page),
      {
        dataType: 'json',
        headers: SpiderLongZhu.headers('https://www.longzhu.com'),
      });
    const list = res.data.data.items.map(room => {
      const type = room.game && room.game[0] ? room.game[0].name : null;
      return {
        anchor: {
          room: room.channel.domain,
          info_id: room.channel.id,// 用来获取详细信息的id
          name: room.channel.name,
          gender: 0,
          avatar: room.channel.avatar,
        },
        type,
        data: {
          num_watch: SpiderLongZhu.toNumber(room.viewers),
          num_live: 0,
          num_follower: 0,
          num_gift: 0,
        },
      };
    });
    return {
      getMoreInfo: true,
      nextPage: res.data.data.lastCursor,
      hasNextPage: list.length >= listSize,
      list,
    };
  }

  /**
   * 获取直播间信息
   * @param room
   */
  static async getRoom(room) {
    const res = await SpiderLongZhu.get(
      SpiderLongZhu.roomUrl(room.anchor.info_id),
      {dataType: 'json'});
    room.data.num_follower = SpiderLongZhu.toNumber(
      res.data.RoomSubscribeCount);
    room.data.num_watch = SpiderLongZhu.toNumber(res.data.OnlineCount);
  }

  /**
   * 生成列表页面的网址
   * @param page
   * @return string
   */
  static listPageUrl(page) {
    page = page ? '&start-cursor=' + page : '';
    return 'https://api.longzhu.com/rcs/streams?max-results=36&layoutid=10237&gameid=0&callback=' +
      page;
  }

  /**
   * 生成直播间信息的网址
   * @param id
   * @return string
   */
  static roomUrl(id) {
    return `http://roomapicdn.longzhu.com/room/roomstatus?roomid=${id}&lzv=1`;
  }

}

module.exports = SpiderLongZhu;